package com.xiaolong.api;

import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.ExcessiveAttemptsException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.xiaolong.base.BaseController;
import com.xiaolong.model.User;



@RestController
public class HomeController extends BaseController {
	
	@RequestMapping({ "/", "index" })  
    public ModelAndView index() {  
        return new ModelAndView("/index");  
    }
	
	@RequestMapping(value={"/login"},method=RequestMethod.GET)
    public ModelAndView login(Model model,String kickout){
		model.addAttribute("user", new User());
		model.addAttribute("kickout",kickout);
		return new ModelAndView("/login");
    }
	
	@RequestMapping(value="/login",method=RequestMethod.POST)
	public ModelAndView login(Model model,HttpServletRequest request){
		/*if(bindingResult.hasErrors()){
			return "/login";
        }*/
		//String userName = user.getUserName();
		//UsernamePasswordToken token = new UsernamePasswordToken(userName, user.getPassword());
		//Subject currentUser = SecurityUtils.getSubject();
		
		//System.out.println("sssss:"+currentUser.getPrincipal());
		String exception = (String) request.getAttribute("shiroLoginFailure"); 
		System.out.println("exception:"+exception);
		if(exception!=null){
			if(UnknownAccountException.class.getName().equals(exception)){
				model.addAttribute("message", "未知账户");
			}else if(IncorrectCredentialsException.class.getName().equals(exception)){
				model.addAttribute("message", "密码不正确");
			}else if(LockedAccountException.class.getName().equals(exception)){
				model.addAttribute("message", "账户已锁定");
			}else if(ExcessiveAttemptsException.class.getName().equals(exception)){
				model.addAttribute("message", "用户名或密码错误次数过多");
			}else if("kaptchaValidateFailed".equals(exception)){
				model.addAttribute("message", "验证码错误");
			}else if(AuthenticationException.class.getName().equals(exception)){
				model.addAttribute("message", "未知账户");
			}
		}
		return new ModelAndView("/login");
	}
	
	@RequestMapping(value="/logout",method=RequestMethod.GET)  
    public String logout(Model model,HttpServletRequest request){ 
        //使用权限管理工具进行用户的退出，跳出登录，给出提示信息
        SecurityUtils.getSubject().logout(); 
        if(request.getSession().getAttribute("kickout")!=null){
        	model.addAttribute("message", "该账户已在其他地方登录！");  
        }
        model.addAttribute("message", "您已安全退出");  
        return "redirect:/login";
    }
	
	@RequestMapping(value="/lock",method=RequestMethod.GET)  
    public ModelAndView lock(RedirectAttributes redirectAttributes ){ 
        //使用权限管理工具进行用户的退出，跳出登录，给出提示信息
        SecurityUtils.getSubject().logout();  
        redirectAttributes.addFlashAttribute("message", "您已安全退出");  
        return new ModelAndView("redirect:/lock");
    }
	
	@RequestMapping(value="/validateTuch",method=RequestMethod.POST)
	public String validateTuch(Model model,HttpServletRequest request){
		Map<String, String[]> map = request.getParameterMap(); 
		Iterator<String> it = map.keySet().iterator();
		while(it.hasNext()){
			String key = it.next();
			String[] val = map.get(key);
			System.out.println(key+":"+val[0]);
		}
		return "";
	}
	
	@RequestMapping(value="/views/{page}",method=RequestMethod.GET)
	public ModelAndView home(@PathVariable String page){
		return new ModelAndView("/views/"+page);
	}
}
